//
// Created by 92304 on 2021/5/19.
//
#include <bits/stdc++.h>

using namespace std;

class Solution {
public:
    int kthLargestValue(vector<vector<int>> &matrix, int k) {
        int M = matrix.size();
        int N = matrix[0].size();
        vector<int> result;
        result.push_back(matrix[0][0]);
        for (int i = 1; i < N; ++i) {
            matrix[0][i] ^= matrix[0][i - 1];
            result.push_back(matrix[0][i]);
        }
        for (int i = 1; i < M; ++i) {
            matrix[i][0] ^= matrix[i - 1][0];
            result.push_back(matrix[i][0]);
        }
        for (int i = 1; i < M; ++i) {
            for (int j = 1; j < N; ++j) {
                int tmp = matrix[i][j] ^matrix[i - 1][j - 1] ^matrix[i - 1][j] ^matrix[i][j - 1];
                matrix[i][j] = tmp;
                result.push_back(tmp);
            }
        }
        sort(result.begin(), result.end(), greater<>());
        return result[k - 1];
    }
};

int main() {
    Solution solution;
    vector<vector<int>> matrix = {{5, 2},
                                  {1, 6}};
    cout << solution.kthLargestValue(matrix, 4) << endl;
}

